﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using HtmlAgilityPack;

namespace Travan.ServerServices.CustomObjects
{
    public class TravianSession
    {
        private HtmlDocument _document;
        public HtmlDocument CurrentDocument { 
            get
            {
                return _document;
            }
            set
            {
                _document = value;
                FormValues = GetFormValues(value);


            } }
        public CookieCollection cookies { get; set; }
        public string Server { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public Dictionary<string, string> FormValues { get; set; }

        public TravianSession(){}


        //Methods to get FormValues from the Document;
        private Dictionary<string,string> GetFormValues(HtmlDocument doc)
        {
            var returnvalue = new Dictionary<string, string>();

            //Get All inputs (textboxes, radiobuttons)
            var inputs = doc.DocumentNode.Descendants("input");
            foreach (var element in inputs)
            {
                string name = element.GetAttributeValue("name", "");
                string value = element.GetAttributeValue("value", "");
                string type = element.GetAttributeValue("type", "");            

                if (string.IsNullOrEmpty(name)) continue;

                switch (type.ToLower())
                {
                    case "checkbox":
                    case "radio":
                        if (!returnvalue.ContainsKey(name)) returnvalue.Add(name, "");
                        string isChecked = element.GetAttributeValue("checked", "unchecked");
                        if (!isChecked.Equals("unchecked")) returnvalue[name] = value;
                        break;
                    case "image":
                        if (!returnvalue.ContainsKey(name))
                        {
                            returnvalue.Add(name+".x", "0");
                            returnvalue.Add(name+".y", "0");
                            returnvalue.Add(name, value);
                        }
                        break;
                        
                    default:
                        returnvalue.Add(name, value);
                        break;
                }          
            }
            //handle dropdownlists (selects)
            var selects = doc.DocumentNode.Descendants("select");
            foreach (var element in selects)
            {
                string name = element.GetAttributeValue("name", "");
                var options = element.Descendants("option");

                if (string.IsNullOrEmpty(name)) continue;

                // choose the first option as default
                var firstOp = options.First();
                string defaultValue = firstOp.GetAttributeValue("value", firstOp.NextSibling.InnerText);

                returnvalue.Add(name, defaultValue);

                // check if any option is selected
                foreach (var option in options)
                {
                    string selected = option.GetAttributeValue("selected", "notSelected");
                    if (!selected.Equals("notSelected"))
                    {
                        string selectedValue = option.GetAttributeValue("value", option.NextSibling.InnerText);
                        returnvalue[name] = selectedValue;
                    }
                }
            }

            return returnvalue;
        }
    }
}
